package com.ultrawise.bank.implement.dao;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import com.ultrawise.bank.base.dao.IDataInsertTools;
import com.ultrawise.bank.dao.assistTools.DataConvert;
import com.ultrawise.bank.dao.assistTools.FileOperation;

public class XMLInsertTools implements IDataInsertTools{

	/**
	 * @param fileName The fileName of data table name.
	 * @param args The args is shaped like "key:value"'s string array.
	 * @return The return result as true or false if insert success or failure.
	 *    
	 */
	@Override
	public boolean insertThree(String fileName, String... args) {
		// TODO Auto-generated method stub
		
		Document doc = FileOperation.getFileDocument(fileName);  //Create DOM tree.
		//doc.normalizeDocument();
		if(doc == null)
		{
			return false;
		}
		
		Element table = doc.getDocumentElement();
		NodeList records = table.getChildNodes();
		Element lastRecord = null;
		for(int i = records.getLength() -1; i >= 0; i --)
		{
			if(records.item(i) instanceof Element)
			{
				lastRecord = (Element)records.item(i);
			}
		}
		
		//Create a node as new record.
		Element newRecord = doc.createElement(lastRecord.getNodeName());
 
		//Take new record insert table.
		table.appendChild(newRecord);  
		
		//Give new record add some fileds and give filed assignment.
		Element filed = null;
		Text content = null;	
		for(int i = 0; i < args.length; i ++)  
		{
			filed = doc.createElement(DataConvert.keyValueSeparate(args[i], 0));
			newRecord.appendChild(filed);
			content = doc.createTextNode(DataConvert.keyValueSeparate(args[i], 1));
			filed.appendChild(content);
		}
		
		//Save modify.
		boolean issave = FileOperation.saveDocument(doc, fileName);  
		if(issave)
		{
			return true;
		}else
		{
			return false;
		}
		
	}

	/**
	 * 
	 * @param fileName The fileName of data table name.
	 * @param pathExpression The parameter is XPath expression.
	 * @param distinguish The parameter is used 
	 * @param args The args is shaped like "key:value"'s string array.
	 * @return The return result as true or false if insert success or failure.
	 */
	@Override
	public boolean insertXP(String fileName, String pathExpression, String... args) 
	{
		// TODO Auto-generated method stub
		
		Document doc = FileOperation.getFileDocument(fileName);  //Create DOM tree.
		if(doc == null)
		{
			return false;
		}
		
		XPathFactory xpf = XPathFactory.newInstance();
		XPath xpath = xpf.newXPath();
		Node target = null;
		try
		{
			target = (Node)xpath.evaluate(pathExpression, doc, XPathConstants.NODE);
		}catch(XPathException xpe)
		{
			xpe.printStackTrace();
		}
		
		//Create a node as new record.
		Element newRecord = null;
		if(target instanceof Element)
		{
			newRecord = doc.createElement(target.getNodeName()); System.out.println(newRecord.getNodeName());
		}else
		{
			return false;
		}
		
		Element parent = (Element)target.getParentNode();
		parent.appendChild(newRecord);
		 	
		//Give new record add some fileds and give filed assignment.
		Element filed = null;
		Text content = null;	
		for(int i = 0; i < args.length; i ++)  
		{
			filed = doc.createElement(DataConvert.keyValueSeparate(args[i], 0));
			newRecord.appendChild(filed);
			content = doc.createTextNode(DataConvert.keyValueSeparate(args[i], 1));
			filed.appendChild(content);
		}
		
		//Save modify.
		boolean issave = FileOperation.saveDocument(doc, fileName);  
		if(issave)
		{
			return true;
		}else
		{
			return false;
		}
	}
	
	public static void main(String[] args)
	{
		XMLInsertTools x = new XMLInsertTools();
		x.insertXP("userInfo", "/UserInfo/user", "id:2", "zhog:33", "xiao:99");
		//x.insert("userInfo", "id:2", "zhog:33", "xiao:99");
	}

}

	

	

